﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using Pixysoft.Framework.Verifications;

namespace Pixysoft.Framework.Noebe
{
    public class NoebeParameterCollection
    {
        int currentIndex = 0;

        List<NoebeKeyValuePair<string, DotNetDataType>> paraTypeList = new List<NoebeKeyValuePair<string, DotNetDataType>>();//如果为空，就是旧操作

        List<List<NoebeKeyValuePair<string, object>>> valueList = new List<List<NoebeKeyValuePair<string, object>>>();//不使用Queue因为不能序列化

        public NoebeParameterCollection()
        {
            currentIndex = 0;
        }


        /// <summary>
        /// 添加字段类型
        /// </summary>
        /// <param name="p"></param>
        /// <param name="type"></param>
        public void AddType(string key, Type type)
        {
            if (!Verification.Verifies(key, type).IsNotNullOrEmpty().Result())
                throw Exceptions.VerificationFailedException(key, type);

            key = key.Trim().ToUpper();

            paraTypeList.Add(new NoebeKeyValuePair<string, DotNetDataType>(key, Pixysoft.Tools.ParserHelper.GetDataTypeByType(type)));
        }

        /// <summary>
        /// 当前层 添加
        /// </summary>
        /// <param name="paraName"></param>
        /// <param name="value"></param>
        public void Add(string key, object value)
        {
            if (!Verification.Verifies(key).IsNotNullOrEmpty().Result())
                throw Exceptions.VerificationFailedException(key);

            key = key.Trim().ToUpper();

            if (ContainsKey(key))
            {
                throw Exceptions.VerificationFailedException(key, value);
            }
            else
            {
                CurrentParaList.Add(new NoebeKeyValuePair<string, object>(key, value));
            }
        }

        /// <summary>
        /// 当前层次的List
        /// </summary>
        internal List<NoebeKeyValuePair<string, object>> CurrentParaList
        {
            get
            {
                if (valueList.Count - 1 < currentIndex)//延迟加载
                {
                    valueList.Add(new List<NoebeKeyValuePair<string, object>>());
                }

                return valueList[currentIndex];
            }
        }

        /// <summary>
        /// 当前层 删除全部
        /// </summary>
        public void Clear()
        {
            CurrentParaList.Clear();
        }

        /// <summary>
        /// 所有层 删除 所有字段类型删除
        /// </summary>
        public void DeepClear()
        {
            valueList.Clear();

            paraTypeList.Clear();

            valueList.Add(new List<NoebeKeyValuePair<string, object>>());

            currentIndex = 0;
        }

        /// <summary>
        /// 调整最后一列参数为存在，跳过空参数
        /// </summary>
        internal void Adjusting()
        {
            //删除中间的空行

            List<List<NoebeKeyValuePair<string, object>>> removelist = new List<List<NoebeKeyValuePair<string, object>>>();

            foreach (List<NoebeKeyValuePair<string, object>> list in valueList)
            {
                if (list == null || list.Count == 0)
                    removelist.Add(list);
            }

            foreach (List<NoebeKeyValuePair<string, object>> list in removelist)
            {
                valueList.Remove(list);
            }

            currentIndex = valueList.Count - 1 < 0 ? 0 : valueList.Count - 1;

            //指针指向第一个有数据行

            while (CurrentParaList.Count == 0 && UnLoad())
            {
            }
        }

        /// <summary>
        /// 是否包含当前字段
        /// </summary>
        /// <param name="paraName"></param>
        /// <returns></returns>
        public bool ContainsKey(string key)
        {
            if (!Verification.Verifies(key).IsNotNullOrEmpty().Result())
                throw Exceptions.VerificationFailedException(key);

            foreach (NoebeKeyValuePair<string, object> pair in CurrentParaList)
            {
                if (string.Equals(pair.Key, key, StringComparison.OrdinalIgnoreCase))
                    return true;
            }

            return false;
        }


        /// <summary>
        /// 当前层 删除index的pair
        /// </summary>
        /// <param name="index"></param>
        public void RemoveAt(int index)
        {
            CurrentParaList.RemoveAt(index);
        }

        /// <summary>
        /// 当前层 根据字段名取值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public object this[string key]
        {
            get
            {
                if (!Verification.Verifies(key).IsNotNullOrEmpty().Result())
                    throw Exceptions.VerificationFailedException(key);

                foreach (NoebeKeyValuePair<string, object> pair in CurrentParaList)
                {
                    if (string.Equals(pair.Key, key, StringComparison.OrdinalIgnoreCase))
                        return pair.Value;
                }

                return null;
            }
        }


        /// <summary>
        /// 清空参数，重新加载，适用于序列化与反序列化
        /// </summary>
        /// <param name="typelist"></param>
        /// <param name="totallist"></param>
        public void ReLoad(List<NoebeKeyValuePair<string, DotNetDataType>> typelist, List<List<NoebeKeyValuePair<string, object>>> paralist)
        {
            if (!Verification.Verifies(typelist, paralist).IsNotNullOrEmpty().Result())
                throw Exceptions.VerificationFailedException(typelist, paralist);

            DeepClear();

            this.paraTypeList = typelist;

            this.valueList = paralist;
        }

        /// <summary>
        /// 当前层装载 切换到下层,只有当前层已经填充数据之后才使用
        /// </summary>
        public bool OnLoad()
        {
            if (CurrentParaList.Count == 0)
                return false;

            valueList.Add(new List<NoebeKeyValuePair<string, object>>());

            currentIndex++;

            return true;
        }

        /// <summary>
        /// 降级,直到最近一个有效数据
        /// </summary>
        internal bool UnLoad()
        {

            if (currentIndex <= 0)
                return false;

            currentIndex--;

            return true;
        }

        /// <summary>
        /// 09-10-31 刷新指针到0
        /// </summary>
        public void Reset()
        {
            currentIndex = 0;
        }

        /// <summary>
        /// 09-10-31 下一行 如果没有返回false
        /// </summary>
        /// <returns></returns>
        public bool Next()
        {
            if (currentIndex >= valueList.Count - 1)
                return false;

            currentIndex++;

            return true;
        }




        /// <summary>
        /// 暴露出来的方法，全部集合被取出
        /// </summary>
        public List<List<NoebeKeyValuePair<string, object>>> ValueList
        {
            get { return valueList; }
        }

        /// <summary>
        /// 即时生成：字段类型的属性
        /// </summary>
        public List<NoebeKeyValuePair<string, DotNetDataType>> TypeList
        {
            get
            {
                return this.paraTypeList;
            }
        }
    }

    public class NoebeKeyValuePair<K, V>
    {
        K _key;
        V _value;
        public NoebeKeyValuePair()
        {
        }

        public NoebeKeyValuePair(K key, V value)
        {
            this._key = key;
            this._value = value;
        }

        [XmlAttribute]
        public K Key
        {
            get { return _key; }
            set { _key = value; }
        }
        public V Value
        {
            get { return _value; }
            set { _value = value; }
        }
    }
}
